﻿using Eleven_Mouth.Model;
using MD5Hash;

namespace Eleven_Mouth.DAL
{
    public class UserDal
    {
        private readonly Mycontext context;

        public UserDal(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 不重复添加
        /// </summary>
        /// <param name="G"></param>
        /// <returns></returns>
        public int UserAdd(UserInfo G)
        {
            try
            {
                var Free = context.Users.Count(F => F.UserName == G.UserName);
                if (Free > 0)
                {
                    return -1;
                }
                else
                {
                    G.Password = G.Password.GetMD5();
                    context.Users.Add(G);
                    return context.SaveChanges();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="UserName"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public dynamic UserLoging(string UserName, string Password)
        {
            try
            {
                var Free = context.Users.Count(D => D.UserName == UserName);
                if (Free == 0)
                {
                    return -2;
                }
                Password = Password.GetMD5();
                var Dream= context.Users.Count(D => D.Password == Password);
                if (Dream == 0)
                {
                    return -1;
                }
                return context.Users.Where(D => D.UserName == UserName&& D.Password == Password).First();
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
